﻿using System;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.Schedule
{
    /// <summary>
    /// TaskDetail 的摘要说明。
    /// </summary>
    public partial class TaskDetailPage : Page
    {
        protected string Date;
        protected string TaskID;
        protected string Username;

        protected override void OnInit(EventArgs e)
        {
            Username = SiteContext.Current.UserName;
            TaskID = (Request.QueryString["TaskID"] != null) ? Request.QueryString["TaskID"] : "0";
            Date = (Request.QueryString["Date"] != null) ? Request.QueryString["Date"] : DateTime.Today.ToShortDateString();

            btnAddCom.Click += btnAddCom_Click;
            btnDelete.Click += btnDelete_Click;
            btnAccept.Click += btnAccept_Click;
            btnFinish.Click += btnFinish_Click;
            btnCancel.Click += btnCancel_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            var task = new Task();
            if (!Page.IsPostBack)
            {
                TaskClass tsk = task.GetTaskDetail(Int32.Parse(TaskID));
                if (tsk.ArrangedBy == Username)
                {
                    btnDelete.Visible = true;
                    btnFinish.Visible = true;
                    btnCancel.Visible = false;
                    btnAccept.Visible = false;
                }


                PopulateData(tsk);

                TaskCommentFrm.Attributes["src"] = "TaskComment.aspx?TaskID=" + TaskID;
                btnFinish.Attributes["onclick"] = "javascript:return confirm('您确认吗?')";
                btnDelete.Attributes["onclick"] = "javascript:return confirm('您确认要删除此任务吗?')";
            }

            SetStatus();
            int status = task.GetTaskStatusBySomeone(Int32.Parse(TaskID), Username);
            if (status == 2)
            {
                btnAccept.Enabled = false;
                btnCancel.Enabled = false;
                btnFinish.Enabled = false;
            }

            base.OnLoad(e);
        }

        private void SetStatus()
        {
            string userName, actualUserName;
            if (Session["Username"] != null)
            {
                userName = Session["Username"].ToString();
                actualUserName = Session["ActualUsername"].ToString();
            }
            else
            {
                userName = SiteContext.Current.UserName;
                actualUserName = SiteContext.Current.UserName;
            }
            txtComment.Enabled = (actualUserName.ToLower() == userName.ToLower());
            btnAccept.Enabled = (actualUserName.ToLower() == userName.ToLower());
            btnCancel.Enabled = (actualUserName.ToLower() == userName.ToLower());
            btnFinish.Enabled = (actualUserName.ToLower() == userName.ToLower());
            btnDelete.Enabled = (actualUserName.ToLower() == userName.ToLower());
            btnAddCom.Enabled = (actualUserName.ToLower() == userName.ToLower());
        }

        private void PopulateData(TaskClass tsk)
        {
            string ClassName = "", Type = "";

            switch (tsk.Type)
            {
                case 1:
                    Type = "会议";
                    break;
                case 2:
                    Type = "文案";
                    break;
                case 3:
                    Type = "来访";
                    break;
                case 4:
                    Type = "电话";
                    break;
                case 5:
                    Type = "走访";
                    break;
                case 6:
                    Type = "外出";
                    break;
                case 7:
                    Type = "假期";
                    break;
                case 8:
                    Type = "出差";
                    break;
            }


            lblSubject.Text = tsk.Subject;
            lblDetail.Text = tsk.Detail;
            lblStartTime.Text = (DateTime.Parse(tsk.StartTime).ToShortTimeString() == "0:00") ? DateTime.Parse(tsk.StartTime).ToShortDateString() + " 8:00" : tsk.StartTime;
            lblEndTime.Text = (DateTime.Parse(tsk.EndTime).ToShortTimeString() == "0:00") ? DateTime.Parse(tsk.EndTime).ToShortDateString() + " 18:00" : tsk.EndTime;
            lblArrangedBy.Text = Components.Staff.GetRealNameByUsername(tsk.ArrangedBy);
            lblCooperator.Text = Components.Staff.GetRealNameForStaffList(tsk.CooperatorList);

            lblType.Text = Type;
            lblAttribute.Text = tsk.Attribute.ToString() == "1" ? "独占任务" : "限时任务";
            if (tsk.ProjectID != 0)
            {
                var prj = new ProjectClass();
                using (SqlDataReader dataReader = prj.GetProjectDetail(tsk.ProjectID))
                {
                    if (dataReader.Read())
                    {
                        ClassName = dataReader["ClassName"].ToString();
                    }
                    dataReader.Close();
                }
            }
            lblProjectID.Text = (tsk.ProjectID == 0) ? "无" : "<a href='#' onclick=javascript:window.close();window.dialogArguments.location='../UnitiveDocument/Project.aspx?classID=" + tsk.ProjectID + "'>" + ClassName + "</a>";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var task = new Task();
            try
            {
                task.DeleteTask(Int32.Parse(TaskID));
                Response.Write("<script language=javascript>alert('删除成功!');window.dialogArguments.location='TaskList.aspx?displayType=" + TaskListPage.displayType + "';window.close();</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            var task = new Task();
            try
            {
                task.DealTask(TaskID, 2, Username, Date);
                Response.Write("<script language=javascript>alert('任务完成操作成功!');window.dialogArguments.location='TaskList.aspx?displayType=" + TaskListPage.displayType + "';window.close();</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            var task = new Task();
            try
            {
                task.DealTask(TaskID, 1, Username, Date);
                Response.Write("<script language=javascript>alert('任务接受操作成功!');window.dialogArguments.location='TaskList.aspx?displayType=" + TaskListPage.displayType + "';window.close();</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            var task = new Task();
            try
            {
                task.DealTask(TaskID, 0, Username, Date);
                Response.Write("<script language=javascript>alert('任务取消操作成功!');window.dialogArguments.location='TaskList.aspx?displayType=" + TaskListPage.displayType + "';window.close();</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        private void btnAddCom_Click(object sender, EventArgs e)
        {
            var task = new Task();
            try
            {
                task.AddTaskComment(Username, txtComment.Text, Int32.Parse(TaskID));
                Response.Write("<script language=javascript>alert('评论添加成功!');</script>");
                TaskCommentFrm.Attributes["src"] = "TaskComment.aspx?TaskID=" + TaskID;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }
    }
}